package session.stateless;

import entity.ActivityAddressEntity;
import entity.AttractionEntity;
import exception.ExistException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.ejb.Remove;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import util.entityhelper.AttractionState;
import util.enumeration.ActivityType;
import util.enumeration.SingaporeArea;

/**
 *
 * @author Swee Zhi
 */
@Stateless
@LocalBean
public class ActivitySessionBean {

    @PersistenceContext
    EntityManager em;
    
    private String selectedLocale;

    private AttractionEntity attraction;
//    private EventEntity event;
    
    // EXCEPTION
    private SystemMessagesBean systemMessages;

    /** Creates a new instance of ActivitySessionBean */
    public ActivitySessionBean() {
        systemMessages = new SystemMessagesBean();
    }
    
    public void setSelectedLocale(String locale) {
        this.selectedLocale = locale;
    }
    
    
    /* FOR ATTRACTION */
   public void createAttraction(ActivityType activityType, SingaporeArea singaporeArea,
            String activityName, String activityDesc, Time openingHour, Time closingHour,
            String imgUrl, int postalCode, String blkNumber, String unitNumber, 
            String streetName, String buildingName)
            throws ExistException {

        ActivityAddressEntity activityAddress = new ActivityAddressEntity();
        activityAddress.setPostalCode(Integer.toString(postalCode));
        activityAddress.setBlkNumber(blkNumber);
        activityAddress.setUnitNumber(unitNumber);
        activityAddress.setStreetName(streetName);
        activityAddress.setBuildingName(buildingName);
        
        
        attraction = new AttractionEntity();
        attraction.setActivityType(activityType);
        attraction.setSingaporeArea(singaporeArea);
        attraction.setActivityName(activityName);
        attraction.setActivityDesc(activityDesc);
        attraction.setOpeningHour(openingHour);
        attraction.setClosingHour(closingHour);
        attraction.setImgUrl(imgUrl);
        attraction.setVenue(activityAddress);
        em.persist(attraction);
        em.flush();
    }

    
    public AttractionState getAttractionByID(Long activityId)
           throws ExistException {
        
        AttractionState attractionState = null;

        // find account id
        Query q = em.createQuery("SELECT a FROM Attraction a WHERE a.id = :attrId");
        q.setParameter("attrId", activityId);

        if (q.getResultList().isEmpty()) {
            // show exception message using SystemMessage bean
            throw new ExistException(systemMessages.getSystemMessage("TOURIST_ACCT_DOES_NOT_EXIST", selectedLocale));
        }
        else {
            AttractionEntity a = (AttractionEntity)q.getSingleResult();
            attractionState = new AttractionState(
                    a.getId(),
                    a.getLocale(),
                    a.getActivityType(),
                    a.getActivityName(),
                    a.getActivityDesc(),
                    a.getSingaporeArea(),
                    a.getVenue(),
                    a.getOpeningHour(),
                    a.getClosingHour(),
                    a.getImgUrl(),
                    a.getLocalizedActivity());
        }
        return attractionState;
    }
    
    public List<AttractionState> getAttractionsReturnByAttractionState() throws ExistException {
        Query q = em.createQuery("SELECT a FROM Attraction a");
        List stateList = new ArrayList();

        if (q.getResultList().isEmpty()) {
            throw new ExistException(systemMessages.getSystemMessage("ATTRACTIONS_DOES_NOT_EXIST", selectedLocale));
        }
        else {
            for (Object o: q.getResultList()) {
                AttractionEntity a = (AttractionEntity)o;
                AttractionState attractionState = new AttractionState(
                        a.getId(),
                        a.getLocale(),
                        a.getActivityType(),
                        a.getActivityName(),
                        a.getActivityDesc(),
                        a.getSingaporeArea(),
                        a.getVenue(),
                        a.getOpeningHour(),
                        a.getClosingHour(),
                        a.getImgUrl(),
                        a.getLocalizedActivity());
                stateList.add(attractionState);
            }
        }
        return stateList;
    }
    
    public List<AttractionEntity> getAttractionsReturnByAttractionEntity() throws ExistException {
        Query q = em.createQuery("SELECT a FROM Attraction a");

        if (q.getResultList().isEmpty()) {
            throw new ExistException(systemMessages.getSystemMessage("ATTRACTIONS_DOES_NOT_EXIST", selectedLocale));
        }
        else {
            return q.getResultList();
        }
    }
    
    
    @Remove
    public void remove() {
        System.out.println("ActivitySessionBean:remove()");
    }
    
}
